package org.an.proj.contacts.ui.side;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import butterknife.ButterKnife;
import butterknife.OnClick;
import java.util.List;
import org.an.ku.base.BaseFragment;
import org.an.ku.view.MyProgressBar;
import org.an.proj.contacts.R;
import org.an.proj.contacts.db.DBManager;
import org.an.proj.contacts.eventbus.EventController;
import org.an.proj.contacts.model.ContactInfo;
import org.an.proj.contacts.model.PhoneInfo;
import org.an.proj.contacts.ui.contact.GroupManagerActivity;

/**
 * A simple {@link Fragment} subclass.
 */
public class SlidingFragment extends BaseFragment {

  private SystemCopyHelper mHelper;
  private MyProgressBar mProgressBar;

  public SlidingFragment() {
    // Required empty public constructor
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_sliding, container, false);
    ButterKnife.bind(this, view);
    return view;
  }

  @Override public void onDestroyView() {
    if (mProgressBar != null) mProgressBar.dismiss();
    if (mHelper != null) {
      mHelper.cancel();
    }
    super.onDestroyView();
    ButterKnife.unbind(this);
  }

  @OnClick(R.id.rl_group_manager) public void onGroupManager() {
    // 分组管理
    EventController.postToggleSlidingEvent(false);
    Bundle bundle = new Bundle();
    bundle.putString("url", "http://www.baidu.com");
    //mIntentFactory.goToOthers(GroupManagerActivity.class);
    Intent intent = new Intent(getActivity(), GroupManagerActivity.class);
    intent.putExtra("intent", "http://www.lianjia.com");
    // putExtras(bundle) 等同于把bundle中的每一个key-value存放到intent（中的bundle）中，
    // 所以在取得时候是可以直接getIntent().getStringExtra("key")取得
    intent.putExtras(bundle);
    startActivity(intent);
  }

  @OnClick(R.id.rl_import_system) public void importSys() {
    if (mHelper == null) {
      mHelper = SystemCopyHelper.newInstance(getActivity(), new SystemCopyHelper.Callback() {
        @Override public void onQueryStart() {
          if (mProgressBar == null) {
            mProgressBar = new MyProgressBar(getActivity());
          }
          mProgressBar.show();
        }

        @Override public void onQueryEnd(List<ContactInfo> list) {
          insert(list);
        }
      });
    }

    mHelper.startQuery();
  }

  private void insert(final List<ContactInfo> list) {
    DBManager.getDaoSession().runInTx(new Runnable() {
      @Override public void run() {
        for (ContactInfo contactInfo : list) {
          long contactId = DBManager.getDaoSession().getContactInfoDao().insert(contactInfo);
          //  保存电话，需要设置联系人id
          if (contactInfo.mLocalPhones != null) {
            for (int i = 0; i < contactInfo.mLocalPhones.size(); i++) {
              PhoneInfo phoneInfo = contactInfo.mLocalPhones.get(i);
              if (phoneInfo.isInvalid()) continue;
              phoneInfo.setContactId(contactId);
              DBManager.getDaoSession().getPhoneInfoDao().insert(phoneInfo);
            }
          }
        }
        if (mProgressBar != null) mProgressBar.dismiss();
        EventController.postRefreshContactsEvent();
      }
    });
  }
}
